
//规则条件列表js代码块
var MarkLibListPage = {
		
		//初始化一些东西
		init : function(){
			
		},
		
		//格式化json
		formatJSON : function(layerId){
			var jqItem = $("#" + layerId );
			jqItem.val(formatJson(jqItem.val()));
		},
		
		 /*创建分页实例current*/
	    pagingTooler : new PagingTooler({
	        callbackMethodName : "MarkLibListPage.findMarkLibList",
	        pageSize : 10,
	        firstPageBtnID : "firstPageBtn",
	        lastPageBtnID : "lastPageBtn",
	        pageCodeLayerID : "pageCodeLayer",
	        recordListLayerID : "dataListLayer",
	        showCurrentPageLayerID : "currentPageLayer",
	        showRecordCountLayerID : "recordCountLayer",
	        showPageCountLayerID : "pageCountLayer",
	        showLoadingLayerID : "loadingLayer",
	        showNoRecordLayerID : "recordLayer",
	        pagingToolbarLayerID : "pagingToolbarLayer",
	        pageSizeSelectLayerID : "pageSizeSelectLayer",
	        jumpPageCodeInputID : "pageInput",
	        jumpPageBtnID : "goPageBtn",
	        pageSizeArray : [10,15,20],
	        hideColumnLayerID : "hideColumnLayer",   //隐藏列div的ID
	    }),
	    
	    search : function(){
	    	var forms = $(".searchParameter");
			var params = {};
			for(var i = 0 ; i < forms.length; i ++){
				var form = $(forms[i]);
				var id = form.attr("id");
				id = id.replace("search_","");
				var value = $.trim(form.val());
				if(value == "") continue;
				params[id] = value;
			}
			
			this.pagingTooler.search(params);
			
	    },
	    
	    findMarkLibList : function(){
	    	
	    	var params = this.pagingTooler.getPagingAndSearchParams();
	    	
	     	$.ajax({
			 	url : '/markLib/findMarkLibList',
			 	type :'POST',
			 	data : JSON.stringify(params),
			 	dataType : "json",
			 	contentType : 'application/json; charset=UTF-8',
			 	success:function(rst){
			 		var count = rst.data.total;
			 		var dataList = rst.data.list;
			 		var txt = "";
			 		MarkLibListPage.dataList = dataList;
			 		for(var i = 0 ; dataList != null && i < dataList.length ; i ++){
			 			txt +=MarkLibListPage.createRecordTxt(dataList[i], i)
			 		}
			 		MarkLibListPage.pagingTooler.showInPage(count, txt,"currentNumberPage");
			 	}		
			 });

	    },
	    
	    //创建表格中每一条记录的文本
	    createRecordTxt : function(data , index){
	    	
	    	var txt = "";
	    	  
			txt = '<tr>'+
						'<td><input type="checkbox" class="checkitem"/></td>'+
						'<td>'+CommonUtil.fillBlank(data.libName)+'</td>'+
						'<td>'+CommonUtil.fillBlank(data.remark)+'</td>'+
	    				'<td>'+
		    				'<button onclick="MarkLibListPage.popDetailMarkLibWindow('+index+')"  class="btn btn-mini">查看</button>&nbsp;'+
		    				'<button onclick="MarkLibListPage.popEditMarkLibWindow('+index+')"  class="btn btn-mini">编辑</button>&nbsp;'+
		    				'<button onclick="MarkLibListPage.popDeleteMarkLibWindow('+index+')"  class="btn btn-mini permissionMenu deleteUserBtn">删除</button>&nbsp;'+
    					'</td>'+
	    			  '</tr>';
	    	return txt;
	    },
	    
	    //打开新增弹窗
	    popAddMarkLibWindow : function(){
	    	$("#addMarkLibWindow").modal();
	    },
	    
	    //获取新增参数
	    getAddMarkLibFormParams : function(){
	    	
	    	var params = {};
	    	
	    	params.libName = $.trim($("#add_libName").val());
	    	params.remark = $.trim($("#add_remark").val());
	    	params.markInfoList = $.trim($("#add_markInfoList").val());
			
	    	return params;
	    	
	    },
	    
	    //校验新增的参数
	    validateAddMarkLibFormParams : function(params){
	    	
	    	if(params.libName == ""){
	    		new $.zui.Messager('标签库不能为空', {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
	    		return false;
	    	}
	    	
	    	if(params.markInfoList == ""){
	    		new $.zui.Messager('标签配置信息不能为空', {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
	    		return false;
	    	}
	    	
	    	if(!CommonUtil.isJson(params.markInfoList)){
	    		new $.zui.Messager('标签配置信息不是一个json格式', {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
	    		return false;
	    	}
	    	
	    	//校验的逻辑自己填写,返回true和false,可以弹出一些提示
	    	return true;

	    },
	    
	    //更新
	   addMarkLib : function(){
	    	
	    	var params = this.getAddMarkLibFormParams();
	    	
	    	//校验不通过,直接返回
	    	if(!this.validateAddMarkLibFormParams(params)) return;
	    	
	    	//json格式转对象
	    	params.markInfoList = JSON.parse(params.markInfoList);
	    	
	    	$.ajax({
			 	url : '/markLib/addMarkLib',
			 	type :'POST',
			 	data : JSON.stringify(params),
			 	
			 	beforeSend : function(){
			 		$("#addMarkLibBtn").attr("disabled","disabled");
			 	},
			 	complete: function(){
			 		$("#addMarkLibBtn").removeAttr("disabled","disabled");
			 	},
			 	dataType : "json",
			 	contentType : 'application/json; charset=UTF-8',
			 	success:function(rst){
			 		
			 		if(rst.code == 200){
			 			new $.zui.Messager(rst.message, {icon:'check' ,type: 'success',placement: 'center',time:'2000'}).show();
			 			MarkLibListPage.findMarkLibList();
			 			$(".closeWindowBtn").click();
			 			return;
			 		}
			 		
			 		new $.zui.Messager(rst.message, {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
			 		
			 	}		
			 });
	    	
	    },
	    
	   //弹出编辑窗口
	  popEditMarkLibWindow : function(index){
	     	
	     	//当前编辑对象
	    	this.currentUpdateRecord = this.dataList[index];
	    	
	    	var currentUpdateRecord = this.currentUpdateRecord;
	    	
	     	for(var key in currentUpdateRecord){
	     		
	     		var value = currentUpdateRecord[key];
	     		
	     		if(key == "markInfoList"){
	     			$("#edit_markInfoList").val(formatJson(JSON.stringify(value)));
	     		}else{
	     			$("#edit_" + key).val(value);
	     		}
	     		
	     	}
	     	
	     	$("#editMarkLibWindow").modal();
	     	
	  },
	    
	  //获取编辑窗口的表单参数
      getUpdateMarkLibFormParams : function(){
	    	
	    	var params = {};
	    	
	    	params.libName = $.trim($("#edit_libName").val());
	    	params.remark = $.trim($("#edit_remark").val());
	    	params.markInfoList = $.trim($("#edit_markInfoList").val());
	    	params.id = this.currentUpdateRecord.id + "";
			
	    	return params;
	    	
	    },
	    
	    //校验新增的参数
	    validateUpdateMarkLibFormParams : function(params){
	    	
	    	if(params.libName == ""){
	    		new $.zui.Messager('标签库不能为空', {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
	    		return false;
	    	}
	    	
	    	if(params.markInfoList == ""){
	    		new $.zui.Messager('标签配置信息不能为空', {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
	    		return false;
	    	}
	    	
	    	if(!CommonUtil.isJson(params.markInfoList)){
	    		new $.zui.Messager('标签配置信息不是一个json格式', {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
	    		return false;
	    	}
	    	
	    	//校验的逻辑自己填写,返回true和false,可以弹出一些提示
	    	return true;

	    },
	    
	    //更新
	    updateMarkLib : function(){
	    	
	    	var params = this.getUpdateMarkLibFormParams();
	    	
	    	//校验不通过,直接返回
	    	if(!this.validateUpdateMarkLibFormParams(params)) return;
	    	
	    	//json格式转对象
	    	params.markInfoList = JSON.parse(params.markInfoList);
	    	
	    	$.ajax({
			 	url : '/markLib/updateMarkLib',
			 	type :'POST',
			 	data : JSON.stringify(params),
			 	
			 	beforeSend : function(){
			 		$("#addMarkLibBtn").attr("disabled","disabled");
			 	},
			 	complete: function(){
			 		$("#addMarkLibBtn").removeAttr("disabled","disabled");
			 	},
			 	dataType : "json",
			 	contentType : 'application/json; charset=UTF-8',
			 	success:function(rst){
			 		
			 		if(rst.code == 200){
			 			new $.zui.Messager('修改成功!', {icon:'check' ,type: 'success',placement: 'center',time:'2000'}).show();
			 			MarkLibListPage.findMarkLibList();
			 			$(".closeWindowBtn").click();
			 			return;
			 		}
			 		
			 		new $.zui.Messager(rst.message, {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
			 		
			 	}		
			 });
	    	
	    },
	    
	    //弹出详情窗口
	    popDetailMarkLibWindow : function(index){
	     	
	     	//当前编辑对象
	    	this.currentUpdateRecord = this.dataList[index];
	    	
	    	var currentUpdateRecord = this.currentUpdateRecord;
	    	
	    	var markInfoList = currentUpdateRecord.markInfoList;
	    	
	    	var txt = "";
	    	
	    	for(var i = 0 ; markInfoList && i < markInfoList.length ; i ++){
	    		
	    		var item = markInfoList[i];
	    		
	    		txt += '<span class="label label-primary" style="float:left;margin-left:10px;margin-top:10px;cursor:pointer;" data-toggle="tooltip" title="'+item.valuePath+'">'+item.markName+'</span>';
	    	}
	    	
	     	for(var key in currentUpdateRecord){
	     		
	     		var value = currentUpdateRecord[key];
	     		
	     		if(key == "markInfoList"){
	     			$("#detail_markInfoList").html(txt);
	     		}else{
	     			$("#detail_" + key).val(value);
	     		}
	     		
	     	}
	     	
	     	//或者在初始化时指定
	     	$('[data-toggle="tooltip"]').tooltip({
	     	    placement: 'top'
	     	});
	     	
	     	$("#detailMarkLibWindow").modal();
	     	
	    },
	    
	    //删除弹窗
	    popDeleteMarkLibWindow : function(index){
	    	
	     	//当前编辑对象
	    	this.currentDeleteRecord = this.dataList[index];
	    	
	    	var currentDeleteRecord = this.currentDeleteRecord;
	    	
	    	$("#deleteMarkLibConfirmWindow").modal();
	    	
	    },
	    
	    //确认删除
	    deleteMarkLib : function(){
	    	
	    	var params = {id : this.currentDeleteRecord.id};
	    	
	    	$.ajax({
			 	url : '/markLib/deleteMarkLibById',
			 	type :'POST',
			 	data : params,
			 	beforeSend : function(){
			 		$("#deleteRecordBtn").attr("disabled","disabled");
			 	},
			 	complete: function(){
			 		$(".closeWindowBtn").click();
			 		$("#deleteRecordBtn").removeAttr("disabled","disabled");
			 	},
			 	dataType : "json",
			 	contentType : 'application/x-www-form-urlencoded; charset=UTF-8',
			 	success:function(rst){
			 		
			 		if(rst.code == 200){
			 			new $.zui.Messager('删除成功!', {icon:'check' ,type: 'success',placement: 'center',time:'2000'}).show();
			 			//删除或更新后停留在当前页
			 			MarkLibListPage.pagingTooler.freshPageByDelete();
			 			return;
			 		}
			 		
			 		new $.zui.Messager(''+rst.message, {icon:'times',type: 'danger',placement: 'center',time:'2000'}).show();
			 		
			 	}		
			 });
	    	
	    }
	    
	    
}

$(function(){
	
	//初始化
	MarkLibListPage.init();
	
	//初始化表格
	MarkLibListPage.pagingTooler.initTableList();
	
});
